<?php
/**
 * Created by PhpStorm.
 * User: hooshim
 * Date: 2014-12-03
 * Time: 오전 11:23
 */

header("Content-Type:text/html;charset=utf-8");
header("Cache-Control:no-cache");
header("Pragma:no-cache");

include_once ($_SERVER['DOCUMENT_ROOT']."/inc/conn.inc.php");
include_once ($_SERVER['DOCUMENT_ROOT']."/inc/function.inc.php"); ##property 파일 인크루드됨.

	//## 파라미터 선언
    $findCompany 			= $_POST['findCompany'];

	// Table 이름 선언
	$tbname = "tblPayment";

    $curDt =date("Y-m-d");

    $query = "SELECT CONVERT(varchar(10), A.wDate, 121) wDate, A.planDt, C.companyKR, REPLACE(CONVERT(varchar, CAST(A.amount AS money), 1), '.00', '') amount ";
    $query .= " , RIGHT(D.actionDt,5) actionDt,  REPLACE(CONVERT(varchar, CAST(D.amount AS money), 1), '.00', '') payAmount ";
    $query .= " , RIGHT(D.bankDt,5) bankDt, A.article, A.billCd, CAST(A.payCd as varchar(10))+'^'+CAST(D.paySeq as varchar(10)) as LINK ";
    $query .= " FROM {$tbname} A ";
	$query .= " LEFT JOIN (SELECT * FROM tblPayResult WHERE flag='Y') D ON A.payCd=D.payCd ";
	$query .= " LEFT JOIN tblCompany C ON A.compCd=C.compCd ";
    $query .= "		WHERE A.flag='Y' " .$condition;
	$query .= " ORDER BY A.wDate DESC";

    $logger->debug($query ."<br>[#FROM:" . $G_remote_ip."#]"); ## 쿼리 로그 작성
    $result = makeJSONArray($query,"GRIDDATA");

    $query = "SELECT REPLACE(CONVERT(varchar, CAST(SUM(amt) AS money), 1), '.00', '') sumAmt ";
    $query .= "         , REPLACE(CONVERT(varchar, CAST(SUM(payAmt) AS money), 1), '.00', '') sumPayAmt FROM ( ";
    $query .= "		SELECT SUM(A.amount) amt, 0 payAmt ";
    $query .= "			 FROM {$tbname} A ";
    $query .= "		WHERE A.flag='Y' " .$condition;
    $query .= "		UNION ALL ";
    $query .= "		SELECT 0 amt, SUM(B.amount) payAmt ";
    $query .= "          FROM {$tbname} A LEFT JOIN tblPayResult B ON  A.payCd=B.payCd ";
    $query .= "		WHERE A.flag='Y' AND B.flag='Y' " .$condition;
    $query .= ") P ";

    $logger->debug($query ."<br>[#FROM:" . $G_remote_ip."#]"); ## 쿼리 로그 작성
    $sum = makeJSONArray($query,$tbname);
    $result['SUM'] = $sum;


    $output = json_encode($result);
    $output = urldecode($output);
    echo iconvDecode($output);
    $logger->debug($_SESSION['BID'] ."//" .$output ."<br>[#FROM:" . $G_remote_ip."#]"); ## 결과 로그 작성
?>